<template>
    <div class="login" id="loginCLick">
        <div class="loginMain">
            <div class="loginMainOne">
                <div class="loginMainOneTop" v-if="verificationShow">
                    账号不能为空
                </div>
                <div class="loginMainOneBot">
                    伯通管理端登录
                </div>
            </div>
            <div class="loginMaininputAccout">
                <input type="text" placeholder="账号" :disabled="disabledShow" maxlength="11" v-model="accountVal" @keyup.enter="submit">
            </div>
            <div class="loginMaininputPwd">
                <input type="password" placeholder="密码" :disabled="disabledShow" v-model="passportPwdVal" @keyup.enter="submit">
            </div>
            <!-- <div class="loginMaininputCode">
            <div>
              <input type="text" placeholder="验证码">
            </div>
            <div>
              <img src="" alt="">
            </div>
        </div> -->
            <div class="loginMaininputBut" @click="loginClick">
                登录
            </div>
        </div>
        <!-- 选择企业 -->
        <div class="updatePwd" v-show="updatePwdShow">
            <transition name="slide-fade">
                <div class="updatePwd-content" v-show="updatePwdShow">
                    <div class="up-content-title">
                        <div>
                            选择企业
                        </div>
                        <div @click="cancelClick">
                            X
                        </div>
                    </div>
                    <div class="up-content-main">
                        <ul>
                            <li class="up-contentLi" @click="loginListsClick(index)" v-for="(item,index) in loginLists" :key="index">
                                <div>
                                    <img alt :src="item.orgLogo | orgLogoFun">
                                </div>
                                <div>
                                    {{item.name | nameFun}}
                                </div>
                            </li>
                        </ul>
                    </div>
                    <!-- <div class="up-content-but">
              <div></div>
              <div>
                
              </div>
            </div> -->
                </div>
            </transition>
        </div>
    </div>
</template>
<script>
import { mapActions } from "vuex";
const crypto = require("crypto");
// import baseObj from "assets/base";
export default {
  data() {
    return {
      disabledShow: false,
      loginLists: [],
      updatePwdShow: false,
      accountVal: "",
      passportPwdVal: "",
      passportPwdIdVal: "",
      verificationShow: false,
      loginClickTrue: false, // 按钮防重复
      submitKeyupTrue: false, // 键盘防重复
      accessToken: ""
    };
  },
  filters: {
    orgLogoFun(val) {
      if (val) {
        return val;
      } else {
        return "/static/img/默认图片.png";
      }
    },
    nameFun(val) {
      if (val) {
        if (val.length > 20) {
          var a = val.slice(0, 15);
          var b = val.slice(val.length - 4, val.length - 1);
          return a + "........" + b;
        } else {
          return val;
        }
      }
    }
  },
  created: function() {
    var self = this;
    sessionStorage.removeItem("ORGID");
    sessionStorage.removeItem("accessToken");
    sessionStorage.removeItem("memberId");
    sessionStorage.removeItem("mobile");
    sessionStorage.removeItem("passportPwdId");
    sessionStorage.removeItem("mClicks");
    self.accountVal = "";
    self.passportPwdVal = "";
  },
  watch: {},
  mounted() {
    document.onkeydown = function() {};
  },
  methods: {
    loginListsClick(index) {
      var self = this;
      self.updatePwdShow = false;
      this.$router.push({
        path: "/home"
      });
      self.disabledShow = false;
      sessionStorage.setItem("ORGID", self.loginLists[index].id); // 企业ID
      sessionStorage.setItem("accessToken", self.accessToken); // token
      sessionStorage.setItem("passportPwdId", self.passportPwdIdVal);
      sessionStorage.setItem("ORGName", self.loginLists[index].name);
    },
    cancelClick() {
      var self = this;
      self.updatePwdShow = false;
      self.disabledShow = false;
    },
    // 键盘事件
    submit() {
      this.loginClick();
    },
    // 登录
    loginClick() {
      var self = this;
      if (self.loginClickTrue == false) {
        self.loginClickTrue = true;
        var possible = self.passportPwdVal;
        var shaCode = crypto.createHash("md5");
        // .toUpperCase()
        var md5Str = shaCode.update(possible, "utf-8").digest("hex");
        self
          .getapipassportuserloginpwd(
            JSON.stringify({
              account: self.accountVal,
              passportPwd: md5Str,
              accountType: 1
            })
          )
          .then(data => {
            self.loginClickTrue = false;
            if (data.statusCode == 200) {
              if (data.data) {
                self.passportPwdIdVal = data.data.passportId;
                if (data.data.tokenVo) {
                  self.accessToken = data.data.tokenVo.accessToken;
                }
                if (data.data.orgList) {
                  if (data.data.orgList.length) {
                    self.loginLists = [];
                    self.loginLists = data.data.orgList;
                    if (data.data.orgList.length > 1) {
                      self.updatePwdShow = true;
                      self.disabledShow = true;
                    } else {
                      sessionStorage.setItem("ORGID", self.loginLists[0].id); // 企业ID
                      sessionStorage.setItem("accessToken", self.accessToken); // token
                      sessionStorage.setItem(
                        "passportPwdId",
                        self.passportPwdIdVal
                      );
                      sessionStorage.setItem(
                        "ORGName",
                        self.loginLists[0].name
                      );
                      self.$router.push({
                        path: "/home"
                      });
                    }
                  }
                } else {
                  self.$message({
                    type: "info",
                    message: "该用户无权限"
                  });
                }
              }
            } else if (data.statusCode == "200003") {
              self.$message({
                type: "info",
                message: data.statusMessage
              });
              this.$router.push({
                path: "/login"
              });
            } else {
              self.$message({
                type: "info",
                message: data.statusMessage
              });
            }
          })
          .catch(msg => {
            self.loginClickTrue = false;
            self.$message({
              type: "info",
              message: "服务器异常"
            });
          });
      }
    },
    ...mapActions(["getapipassportuserloginpwd"])
  }
};
</script>
<style scoped="">
.loginMainOneTop {
  width: 406px;
  height: 48px;
  background: rgba(234, 98, 98, 0.4);
  font-size: 16px;
  color: rgba(234, 98, 98, 1);
  text-align: center;
  line-height: 48px;
  position: absolute;
  top: 0px;
  left: 0;
}

.loginMainOneBot {
  height: 36px;
  font-size: 36px;
  color: rgba(67, 144, 225, 1);
  line-height: 36px;
  width: 100%;
  text-align: center;
  position: absolute;
  top: 50px;
  left: 0;
}

.loginMainOne {
  height: 86px;
  position: relative;
  overflow: hidden;
}

.loginMaininputBut {
  width: 358px;
  height: 48px;
  background: rgba(67, 144, 225, 1);
  border-radius: 2px;
  margin: 0 auto;
  font-size: 18px;
  color: rgba(255, 255, 255, 1);
  margin-top: 50px;
  text-align: center;
  line-height: 48px;
  cursor: pointer;
}

.loginMaininputCode > div:nth-child(1) {
  width: 238px;
  height: 48px;
}

.loginMaininputCode > div:nth-child(2) {
  width: 120px;
  height: 48px;
  background: rgba(199, 199, 199, 1);
  cursor: pointer;
}

.loginMaininputCode > div:nth-child(1) input {
  width: 222px;
  height: 48px;
  padding-left: 16px;
  font-size: 16px;
}

.loginMaininputCode {
  display: flex;
  width: 358px;
  height: 48px;
  border-radius: 2px;
  background-color: #fff;
  margin: 0 auto;
  border: 1px solid #e3e3e3;
  margin-top: 40px;
  display: flex;
  align-items: center;
}

.loginMaininputAccout input,
.loginMaininputPwd input {
  height: 48px;
  line-height: 48px;
  width: 342px;
  padding-left: 16px;
  font-size: 16px;
  color: rgba(44, 44, 44, 1);
}

.loginMaininputAccout {
  display: flex;
  width: 358px;
  height: 48px;
  border-radius: 2px;
  background-color: #fff;
  margin: 0 auto;
  border: 1px solid #e3e3e3;
  margin-top: 40px;
  display: flex;
  align-items: center;
}

.loginMaininputPwd {
  display: flex;
  width: 358px;
  height: 48px;
  border-radius: 2px;
  background-color: #fff;
  margin: 0 auto;
  border: 1px solid #e3e3e3;
  margin-top: 30px;
  display: flex;
  align-items: center;
}

.loginMain {
  position: absolute;
  top: calc(50% - 260px);
  right: 164px;
  background-color: #fff;
  width: 406px;
  height: 460px;
  background: rgba(255, 255, 255, 1);
  border-radius: 2px;
  background-color: #fff;
  border-radius: 2px;
  overflow: hidden;
}

.login {
  width: 100%;
  position: relative;
  min-height: 100vh;
  background: url("/static/img/Grou4.png") no-repeat;
  background-size: 100% 100%;
  background-position: 0 0;
}

.up-content-but {
  height: 60px;
  width: 100%;
  background-color: #f0f6fa;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #e5e5e5;
}

.up-content-but > div:nth-child(2) {
  width: 55px;
  height: 35px;
  border-radius: 4px;
  background-color: #38adff;
  text-align: center;
  line-height: 35px;
  color: #fff;
  margin-right: 25px;
  cursor: pointer;
}

.updatePwd {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
}

.updatePwd-content {
  width: 840px;
  height: 350px;
  background-color: #fff;
  border-radius: 5px;
  overflow: hidden;
}

.up-content-main {
  height: 280px;
  width: 100%;
  border-top: 1px solid #e5e5e5;
  overflow-y: auto;
}

.up-content-main ul {
  width: 86%;
  margin: 0 auto;
  min-height: 50px;
  margin-top: 35px;
  display: flex;
  flex-wrap: wrap;
}

.up-content-title {
  height: 52px;
  width: 100%;
  background-color: #f0f6fa;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.up-content-title > div:nth-child(1) {
  margin-left: 15px;
}

.up-content-title > div:nth-child(2) {
  margin-right: 15px;
  font-size: 16px;
  color: #ccc;
  cursor: pointer;
}

.up-contentLi {
  width: 120px;
  height: 130px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  margin: 10px;
}

.up-contentLi > div:nth-child(1) {
  width: 120px;
  height: 70px;
  display: flex;
  justify-content: center;
}

.up-contentLi > div:nth-child(1) img {
  width: 70px;
  height: 70px;
}

.up-contentLi > div:nth-child(2) {
  width: 120px;
  height: 40px;
  line-height: 20px;
  font-size: 12px;
  color: #000;
  margin-top: 10px;
  text-align: center;
}

input:-webkit-autofill {
  background-color: #fff !important;
  background-image: none !important;
  color: rgb(255, 255, 255) !important;
}

.slide-fade-enter-active {
  transition: all 0.7s ease;
}

.slide-fade-enter,
.slide-fade-leave-active {
  transform: translateY(-30px);
  opacity: 0;
}
</style>